/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.model.geom;

import java.util.ArrayList;
import java.util.List;

import chunmap.model.coord.PrecisionModel;
import chunmap.model.elem.Envelope;

/**
 * 抽象几何体
 * 
 * @author chunquedong
 * 
 */
public abstract class AbstractGeometry implements Geometry {

	private Envelope envelop;
	private Boolean isValid;
	private PrecisionModel precision=PrecisionModel.getDoubleFloatModel();

	@Override
	public final Envelope getEnvelop() {
		if (envelop == null) {
			envelop = calculateEnvelop();
		}
		return envelop;
	}

	public void _setEnvelop(Envelope env) {
		envelop = env;
	}

	protected abstract Envelope calculateEnvelop();

	
	public GeometryCollection toGeometryCollection() {
		if (this instanceof GeometryCollection) {
			return (GeometryCollection) this;
		} else {
			List<Geometry> gs = new ArrayList<Geometry>();
			gs.add(this);
			return new MultiGeometry<Geometry>(gs);
		}
	}

	@Override
	public final boolean isValid() {
		if (isValid == null) {
			isValid = isSimple();
		}
		return isValid.booleanValue();
	}

	public void _setValid(boolean b) {
		isValid = b;
	}

	protected abstract boolean isSimple();

	@Override
	public PrecisionModel getPrecision() {
		return precision;
	}
	@Override
	public abstract boolean equals(Object obj);
	@Override
	public abstract String toString();
}
